package JavaHomework05;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class zuoye08 {
	public static void main(String[] args) {
		ArrayList<String> poker=new ArrayList<String>();
		String[] colors={"方块♦","黑桃♠","红桃♥","梅花♣"}; 
		String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
		poker.add("大王");poker.add("小王");

		for(String number:numbers){
			for(String color:colors){
				poker.add(color+number);
			}
		}
		String[] poker1=poker.toArray(new String[poker.size()]);

		Collections.shuffle(poker);

		Player player1 = new Player("王老");
		Player player2 = new Player("老八");
		Player player3 = new Player("洪七");
		Player player4 = new Player("底牌");

		for(int i=0;i<poker.size();i++){
			String p = poker.get(i);
			if(i<51){
				if(i%3==0){
					player1.getList().add(p);
				}else if(i%3==1){
					player2.getList().add(p);
				}else{
					player3.getList().add(p);
				}
			}else{
				player4.getList().add(p);
			}
		}

		sortPoker(player1.getList(),poker1);
		sortPoker(player2.getList(),poker1);
		sortPoker(player3.getList(),poker1);
		sortPoker(player4.getList(),poker1);
		System.out.println(player1.getName()+":"+player1.getList());
		System.out.println(player2.getName()+":"+player2.getList());
		System.out.println(player3.getName()+":"+player3.getList());
		System.out.println(player4.getName()+":"+player4.getList());
	}

	private static void sortPoker(ArrayList<String> list, String[] poker1) {
		HashMap<Integer,String> map = new HashMap<>();
		for(int i=0;i<poker1.length;i++){
			map.put(i, poker1[i]);
		}
		Collections.sort(list, new Comparator<String>() {
            public int compare(String o1, String o2) {
                int index1 = findKey(map,o1);
                int index2 = findKey(map,o2);
                return index1-index2;
            }
			private int findKey(HashMap<Integer, String> map, String card) {
				for(int key:map.keySet()){
					if(map.get(key).equals(card)){
						return key;
					}
				}
				return -1;
			}
        });
	}
}
